package com.wswy.wyjk.ui.practice.exam

import com.wswy.wyjk.model.PracticeData
import com.wswy.wyjk.model.PracticeType
import com.wswy.wyjk.model.vo.ExamRegularConfig
import com.wswy.wyjk.model.vo.ExamResult
import com.wswy.wyjk.model.vo.PracticeModel
import java.util.concurrent.TimeUnit

object ExamCenter {

    private val qList = ArrayList<PracticeModel>()
    private var practiceType: PracticeType? = null
    private var examConfig: ExamRegularConfig? = null
    private var examTime: Long = 0

    fun fillExams(list: List<PracticeModel>, type: PracticeType) {
        qList.addAll(list)
        practiceType = type
    }

    fun getWrongList():List<PracticeModel>?{
        val retList = ArrayList<PracticeModel>()

        qList.forEach {
            if (it.hasAnswer() && !it.isRight()){
                retList.add(it)
            }
        }

        return retList
    }

    /**
     * 考试结束
     */
    fun saveExamTime(time: Long) {
        examTime = time
    }

    fun saveRegularConfig(exam: ExamRegularConfig) {
        examConfig = exam
    }

    fun calResult(): ExamResult? {
        val type = practiceType ?: return null
        val config = examConfig ?: return null

        var score = 0
        var wrongCount = 0
        var undoneCount = 0


        qList.forEach {

            if (it.hasAnswer()) {
                //已做

                if (it.isRight()) {
                    //正确

                    score += when (it.practiceData.optionType) {
                        PracticeData.TYPE_JUDGE -> config.judgePoint
                        PracticeData.TYPE_RADIO -> config.radioPoint
                        PracticeData.TYPE_MULTIPLE -> config.multiplePoint
                        else -> 0
                    }


                } else {
                    //错误
                    wrongCount++
                }

            } else {
                undoneCount++
            }
        }

        return ExamResult(
            score,
            wrongCount,
            undoneCount,
            TimeUnit.MINUTES.toSeconds(config.examTime) - examTime,
            score >= config.qualifiedPoint,
            type,
            config
        )
    }


    fun clear() {

        qList.clear()
        practiceType = null
        examConfig = null
        examTime = 0
    }

}